package org.system.core.controller.system;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.system.core.annotation.Log;
import org.system.core.common.PageResult;
import org.system.core.common.Result;
import org.system.core.domain.entity.SysDictType;
import org.system.core.enums.BusinessType;
import org.system.core.service.system.SysDictTypeService;
import org.system.core.utils.PageUtils;

import java.util.List;

/**
 * @author baoRoot
 */
@RestController
@Api(tags={"字典类型模块"})
@RequestMapping("/system/dict/type")
public class SysDictTypeController {

    private static final Logger logger = LoggerFactory.getLogger(SysDictTypeController.class);

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @GetMapping("/list")
    @ApiOperation("获取字典类型列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    public PageResult<SysDictType> list(SysDictType dictType) {
        logger.info("获取字典类型列表：{}", dictType);
        PageUtils.startPage();
        List<SysDictType> list = sysDictTypeService.selectDictTypeList(dictType);
        PageInfo<SysDictType> pageInfo = new PageInfo<>(list);
        return PageResult.ok(pageInfo);
    }

    @ApiOperation("查询字典类型详细")
    @GetMapping(value = "/{dictId}")
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    public Result<SysDictType> getInfo(@PathVariable Long dictId) {
        logger.info("查询字典类型详细： {}", dictId);
        return Result.ok(sysDictTypeService.selectDictTypeById(dictId));
    }

    @PostMapping
    @ApiOperation("新增字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public Result<String> addDictType(@Validated @RequestBody SysDictType dictType) {
        logger.info("新增字典类型: {}", dictType);
        sysDictTypeService.insertDictType(dictType);
        return Result.ok();
    }

    @PutMapping
    @ApiOperation("修改字典类型")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    public Result<String> editDictType(@Validated @RequestBody SysDictType dictType) {
        logger.info("修改字典类型： {}", dictType);
        sysDictTypeService.updateDictType(dictType);
        return Result.ok();
    }

    @ApiOperation("删除字段类型")
    @DeleteMapping("/{dictIds}")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    public Result<String> deleteDict(@PathVariable Long[] dictIds) {
        logger.info("删除字段类型: {}", dictIds);
        sysDictTypeService.deleteDictTypeByIds(dictIds);
        return Result.ok();
    }

    @ApiOperation("刷新字典缓存")
    @DeleteMapping("/refreshCache")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    public Result<String> refreshCache(){
        logger.info("刷新字典缓存");
        sysDictTypeService.resetDictCache();
        return Result.ok();
    }

    @GetMapping("/optionSelect")
    @ApiOperation("获取字典选择框列表")
    public Result<List<SysDictType>> optionSelect() {
        logger.info("获取字典选择框列表");
        List<SysDictType> dictTypes = sysDictTypeService.selectDictTypeAll();
        return Result.ok(dictTypes);
    }

}
